package com.webinsky.persist.dao.catalog;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.webinsky.common.utils.db.BasicSqlProvider;
import com.webinsky.common.utils.db.PageHelper;
import com.webinsky.common.utils.db.Pagnation;
import com.webinsky.common.utils.db.SQLBuilder;
import com.webinsky.persist.entity.catalog.TagcatFilter;

public class TagcatProvider extends BasicSqlProvider {

	private String[] cols = new String[]{"id", "site_id", "name", "sort", "language", "del", "create_at", "update_at"};
	private String[] countCols = new String[]{"count(*)"};
	
	public TagcatProvider() {
		super("tagcat");
	}
	
	public String selectByPagnation(Map<String, Object> param){
		Pagnation filter = (Pagnation)param.get("filter");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		int pageStart = filter.getPageStart();
		int pageSize = filter.getPageSize();
		String pageSQL = PageHelper.startPage(sqlBuilder.toString(), pageStart, pageSize);
		return pageSQL;
	}
	
	@SuppressWarnings("unchecked")
	public String selectByIds(Map<String, Object> param){
		List<Integer> ids =  (List<Integer>)param.get("ids");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		sqlBuilder.WHERE("del = 0");
		if ( ids != null && !ids.isEmpty() ){
			List<String> strIds = ids.stream().map(id -> String.valueOf(id)).collect(Collectors.toList());
			sqlBuilder.WHERE(" id in (" + String.join(",", strIds) + ")");
		}
		return sqlBuilder.toString();
	}
	
	public String selectByFilter(Map<String, Object> param){
		TagcatFilter filter = (TagcatFilter)param.get("filter");
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		sqlBuilder.WHERE("del = 0");
		if ( filter != null ){
			if ( filter.getSiteId() > 0 ){
				sqlBuilder.WHERE("site_id = " + filter.getSiteId());
			}
			if ( filter.getLang() != null ){
				sqlBuilder.WHERE("language = '" + filter.getLang() + "'");
			}
		}
		int pageStart = filter.getPageStart();
		int pageSize = filter.getPageSize();
		String pageSQL = PageHelper.startPage(sqlBuilder.toString(), pageStart, pageSize);
		return pageSQL;
	}
	
	public String selectCountByFilter(Map<String, Object> param){
		TagcatFilter filter = (TagcatFilter)param.get("filter");
		SQLBuilder sqlBuilder = getSelectBuilder(countCols);
		sqlBuilder.WHERE("del = 0");
		if ( filter != null ){
			if ( filter.getSiteId() > 0 ){
				sqlBuilder.WHERE("site_id = " + filter.getSiteId());
			}
			if ( filter.getLang() != null ){
				sqlBuilder.WHERE("language = '" + filter.getLang() + "'");
			}
		}
		return sqlBuilder.toString();
	}
	
	@SuppressWarnings("unchecked")
	public String selectBySiteIdAndIds(Map<String, Object> params){
		Integer siteId = (Integer)params.get("siteId");
		List<Integer> tagcatIds = (List<Integer>)params.get("ids"); 
		SQLBuilder sqlBuilder = getSelectBuilder(cols);
		sqlBuilder.WHERE("del = 0");
		if ( siteId != null ){
			sqlBuilder.WHERE("site_id = " + siteId);
		}
		if ( tagcatIds != null && !tagcatIds.isEmpty() ){
			List<String> tagcatStringIds = tagcatIds.stream().map(e -> String.valueOf(e)).collect(Collectors.toList());
			String condition = String.join(",", tagcatStringIds);
			condition = " id in (" + condition + ")";
			sqlBuilder.WHERE(condition);
		}
		return sqlBuilder.toString();
	}
}
